package org.txazo.music.dao.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.txazo.music.dao.FriendDao;

/**
 * 好友数据访问实现
 * 
 * @author Tu XiaoZhou
 * @since 2013-04-12
 */
@Repository("friendDao")
public class FriendDaoImpl implements FriendDao {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
		return namedParameterJdbcTemplate;
	}

	public void setNamedParameterJdbcTemplate(
			NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
		this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
	}

	@Override
	public void addFriend(int id, int userId) {
		String sql = "insert into friend (friendAId, friendBId, makeTime) values (?, ?, ?)";
		namedParameterJdbcTemplate.getJdbcOperations().update(sql,
				new Object[] { id, userId, new Date() });
	}

	@Override
	public void removeFriend(int id, int userId) {
		String sql = "delete from friend where (friendAId = ? and friendBId = ?) "
				+ "or (friendBId = ? and friendAId = ?)";
		namedParameterJdbcTemplate.getJdbcOperations().update(sql,
				new Object[] { id, userId, id, userId });
	}

}
